package com.tengju.bff.interfaces.covercharge.controller.admin;

import com.idanchuang.component.base.JsonResult;
import com.tengju.bff.interfaces.covercharge.constant.CoverChargeErrorCode;
import com.tengju.bff.interfaces.covercharge.entity.vo.RedisVo;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import com.tengju.bff.interfaces.covercharge.service.IRedisService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author cj
 */
@Api(value = "redis", tags = "设置提现缓存")
@RestController
@Slf4j
@RequestMapping("/manage/redisAdmin/redis")
@ManageEntrance
public class RedisController {
    @Autowired
    private IRedisService redisService;

    @PostMapping("/setRedis")
    @ResponseBody
    @ApiOperation(value = "设置提现缓存开关")
    public ApiResponse<?> setRedis(HttpServletRequest request, @RequestBody RedisVo req){
        Assert.notNull(req.getVal(),"缓存值不能为空");

        try {
            redisService.setWithdrawSwitchRedis(req);
            return ApiResponse.newSuccess();
        } catch (Exception e) {
            log.error("设置提现缓存开关报错", e);
            return ApiResponse.newFailure(CoverChargeErrorCode.SYS_ERROR.getCode(), e.getMessage());
        }
    }

    @PostMapping("/queryRedis")
    @ResponseBody
    @ApiOperation(value = "查看提现缓存开关")
    public ApiResponse<?> queryRedis(HttpServletRequest request, @RequestBody RedisVo req){
        try {
            return ApiResponse.newSuccess(redisService.queryWithdrawSwitchRedis());
        } catch (Exception e) {
            log.error("查看提现缓存开关报错", e);
            return ApiResponse.newFailure(CoverChargeErrorCode.SYS_ERROR.getCode(), e.getMessage());
        }
    }

    @PostMapping("/delRedis")
    @ResponseBody
    @ApiOperation(value = "删除提现缓存开关")
    public ApiResponse<?> delRedis(HttpServletRequest request, @RequestBody RedisVo req){
        try {
            redisService.delWithdrawSwitchRedis();
            return ApiResponse.newSuccess();
        } catch (Exception e) {
            log.error("删除提现缓存开关报错", e);
            return ApiResponse.newFailure(CoverChargeErrorCode.SYS_ERROR.getCode(), e.getMessage());
        }
    }
}
